package com.example.shopping_mall.fragment;

import android.content.Intent;
import android.graphics.Bitmap;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.shopping_mall.R;
import com.example.shopping_mall.adapter.RoderAdapter;
import com.example.shopping_mall.application.MineOrderPresenter;
import com.example.shopping_mall.bean.EndMineOrder;
import com.example.shopping_mall.bean.MineOrder;
import com.example.shopping_mall.bean.OrderItemList;
import com.example.shopping_mall.http.IMineOrderPresenter;

import java.util.ArrayList;
import java.util.List;


/**
 * 待发货deliver
 */
public class MineDeliverFragment extends AllFragment {

    private static final String TAG = MineDeliverFragment.class.getSimpleName();

    private RecyclerView deliverRV;

    public IMineOrderPresenter mineOrderPresenter;
    private MineOrder order;
    private Bitmap bitmap;
    private ImageView imageSU;
    private TextView textcommodity;


    OrderItemList itemList;
    EndMineOrder endMineOrder;


    List<Object> listO = new ArrayList<>();

    @Override
    protected void initView() {

        mineOrderPresenter = new MineOrderPresenter(getContext());
        //初始化列表
        deliverRV = findViewById(R.id.deliverRV);
        deliverRV.setLayoutManager(new LinearLayoutManager(getActivity()));

        initData();
    }


    private void initData() {

        mineOrderPresenter.requestOrder(1, new MineOrderPresenter.OnMineOrderListener() {
            @Override
            public void onMineOrder(MineOrder mineOrder) {
                if (mineOrder.getStatus() == 1) {
                    List<List<OrderItemList>> orderList = new ArrayList<>();
                    //System.out.println("订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单': " + mineOrder.getStatus());
                    OrderItemList[] orderItemLists = mineOrder.getOrderItemList();
                    List<OrderItemList> listitem = new ArrayList<>();
                    for (int i = 0; i < orderItemLists.length; i++) {
                        itemList = new OrderItemList();
                        itemList.setProductPrice(orderItemLists[i].getProductPrice());
                        itemList.setProductPic(orderItemLists[i].getProductPic());
                        itemList.setProductName(orderItemLists[i].getProductName());
                        listitem.add(itemList);
                    }
                    orderList.add(listitem);


                    //拆分数据
                    if (orderList != null) {
                        for (int i = 0; i < orderList.size(); i++) {
                            order = new MineOrder();
                            order.setOrderSn(mineOrder.getOrderSn());
                            order.setTotalAmount(mineOrder.getTotalAmount());
                            order.setPayAmount(mineOrder.getPayAmount());
                            order.setStatus(mineOrder.getStatus());

                            endMineOrder = new EndMineOrder();
                            endMineOrder.setOrderSn(mineOrder.getOrderSn());
                            endMineOrder.setTotalAmount(mineOrder.getTotalAmount());
                            endMineOrder.setPayAmount(mineOrder.getPayAmount());
                            endMineOrder.setStatus(mineOrder.getStatus());


                            listO.add(order);
                            listO.addAll(orderList.get(i));
                            listO.add(endMineOrder);
                        }
                    }
                    RoderAdapter adapter = new RoderAdapter(listO, getActivity());
                    deliverRV.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                } else {
                    findViewById(R.id.evliverNullOrder).setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine_deliver;
    }

}